package com.sanron.sunweather.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	
	public static Date parse(String str,String format){
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			date= sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 判断白天黑夜
	 * @param sunrise 日升时间
	 * @param sunset 日落时间
	 * @return
	 */
	public static int dayOrNight(Calendar sunrise,Calendar sunset){
		Calendar calendar = Calendar.getInstance();
		if(calendar.get(Calendar.HOUR_OF_DAY) >=0 && calendar.compareTo(sunrise)<0){
			//昨天的晚上
			return 0;
		}else if( calendar.compareTo(sunrise)>=0 && calendar.compareTo(sunset)<0){
			//今天的白天
			return 1;
		}
		//今天的晚上
		return 2;
	}
}
